<?php
	class EventManager extends Delegation {
		var $FormName;
		var $Events;
		var $Event;
		var $DiscussionID;
		var $Discussion;
		var $DiscussionManager;
		var $CommentID;
		var $Comment;
		var $CommentManager;
		var $CalendarSelect;
		var $RepeatList;
		var $RepeatType;
		var $WeekDays;
		var $WeekDaysCheckboxes;
		var $MonthWeekNum;
		var $CustomFields;

		// Constructor
		function EventManager(&$Context) {
			$this->Name = 'EventManager';
			$this->Delegation($Context);
			$this->FormName = 'frmPostDiscussion';
			$this->Events = array();
			$this->Event = array();
			$this->DiscussionID = ForceIncomingInt('DiscussionID', 0);
			$this->CommentID = ForceIncomingInt('CommentID', 0);
			$this->CalendarSelect = '';
			$this->RepeatList = '';
			$this->RepeatType = array('No' => $this->Context->GetDefinition('VcalNoRepeat'),
						  'Daily' => $this->Context->GetDefinition('VcalDaily'),
						  'Weekly' => $this->Context->GetDefinition('VcalWeekly'),
						  'Monthly' => $this->Context->GetDefinition('VcalMonthly'),
						  'Yearly' => $this->Context->GetDefinition('VcalYearly'),
						  'Custom' => $this->Context->GetDefinition('VcalCustom'));
			$this->WeekDays = array(0 => $this->Context->GetDefinition('VcalSmallSunday'),
						1 => $this->Context->GetDefinition('VcalSmallMonday'),
						2 => $this->Context->GetDefinition('VcalSmallTuesday'),
						3 => $this->Context->GetDefinition('VcalSmallWednesday'),
						4 => $this->Context->GetDefinition('VcalSmallThursday'),
						5 => $this->Context->GetDefinition('VcalSmallFriday'),
						6 => $this->Context->GetDefinition('VcalSmallSaturday'));
			$this->WeekDaysCheckboxes = '';
			$this->MonthWeekNum = '';
			$this->Comment = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Comment');
			$this->Discussion = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Discussion');
			$this->CommentManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'CommentManager');
			$this->DiscussionManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'DiscussionManager');
			$this->DelegateParameters['CommentManager'] = &$this->CommentManager;
			$this->DelegateParameters['DiscussionManager'] = &$this->DiscussionManager;
			$this->CallDelegate('Constructor');
			$this->Event = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Event');
			$this->Events = $this->Event;
		}

		function GetEventsBuilder() {
			$sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
			$sql->SetMainTable('Event', 'Event');
			$sql->AddSelect(array('EventID', 'CalendarID', 'DiscussionID', 'UserID', 'Name', 'RecurID', 'StartDate', 'EndDate','StartTime','EndTime', 'Approved', 'DateCreated','AllDayEvent'), 'Event');
			$sql->AddJoin('Calendar', 'Calendar', 'CalendarID', 'Event', 'CalendarID', 'LEFT JOIN');
			$sql->AddSelect(array('Title', 'Description','Color', 'RequireApproval','Priority'), 'Calendar');
			if ($this->Context->Session->UserID > 0) {
				$sql->AddJoin('CalendarRoleBlock', 'CalendarRoleBlock', 'CalendarID', 'Calendar', 'CalendarID', 'LEFT JOIN', ' AND CalendarRoleBlock.'.$this->Context->DatabaseColumns['CalendarRoleBlock']['RoleID'].' = '.$this->Context->Session->User->RoleID);
				$sql->AddJoin('CalendarBlock', 'CalendarBlock', 'CalendarID', 'Calendar', 'CalendarID', 'left join', ' AND CalendarBlock.'.$this->Context->DatabaseColumns['CalendarBlock']['UserID'].' = '.$this->Context->Session->UserID);
				$sql->AddSelect('Blocked', 'CalendarBlock', 'Blocked', 'COALESCE', '0');
			} else {
				$sql->AddJoin('CalendarRoleBlock', 'CalendarRoleBlock', 'CalendarID', 'Calendar', 'CalendarID', 'LEFT JOIN', ' AND CalendarRoleBlock.'.$this->Context->DatabaseColumns['CalendarRoleBlock']['RoleID'].' = 1');
			}
			// Limit to calendars that this user is allowed to see.
			$sql->AddWhere('CalendarRoleBlock', 'Blocked', '', 0, '=', 'and', '', 1, 1);
			$sql->AddWhere('CalendarRoleBlock', 'Blocked', '', 0, '=', 'or', '', 0);
			$sql->AddWhere('CalendarRoleBlock', 'Blocked', '', 'null', 'is', 'or', '', 0);
			$sql->EndWhereGroup();
		
			return $sql;
		}
		function GetEventByDiscussionID($DiscussionID) {
			$sql = $this->GetEventsBuilder();
			$sql->AddWhere('Event', 'DiscussionID', '', $DiscussionID, '=');
			$ResultSet = $this->Context->Database->Select($sql, $this->Name, 'GetEventByDiscussionID', 'An error occurred while retrieving the event for this discussion.');
			if ($this->Context->Database->RowCount($ResultSet) == 0)
				$this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrEventNotFound'));
			
			$this->Event->GetPropertiesFromDataSet($this->Context->Configuration, $this->Context->Database->GetRow($ResultSet));
			$this->Event->FormatPropertiesForDisplay();

		}
		function RemoveEvent($EventID) {
			$sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
			$sql->SetMainTable('Event', 'Event');
			$sql->AddWhere('Event', 'EventID', '', $EventID, '=');
			$this->Context->Database->Delete($sql, $this->Name, 'RemoveEvent', 'An error occurred while removing event.');
		}
		
		function GetEventByDateRange($StartDate, $EndDate) {
			$sql = $this->GetEventsBuilder();
			$sql->StartWhereGroup('AND');
			$sql->AddWhere('Event', 'StartDate', '', $StartDate, '>=', '', '', 1,1);
			$sql->AddWhere('Event', 'StartDate', '', $EndDate, '<=', 'AND', '', 1,0);
			$sql->EndWhereGroup();
			$sql->AddWhere('Event', 'EndDate', '', $StartDate, '>=', 'OR', '', 1,1);
			$sql->AddWhere('Event', 'EndDate', '', $EndDate, '<=', 'AND', '', 1,0);
			$sql->EndWhereGroup();
			$sql->EndWhereGroup();
			$sql->AddOrderBy('StartDate', 'Event');
			$sql->AddOrderBy('StartTime', 'Event');
			$this->Events = array();
			$ResultSet = $this->Context->Database->Select($sql, $this->Name, 'GetEventByDateRange', 'An error occurred while retrieving events within the specified dates.');
			if ($this->Context->Database->RowCount($ResultSet) == 0)
				$this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrEventNotFound'));
			while ($rows = $this->Context->Database->GetRow($ResultSet)) {
				$this->Event->GetPropertiesFromDataSet($this->Context->Configuration, $rows);
				if ($this->Event->Blocked == 0) {
					$this->Event->FormatPropertiesForDisplay();
					$this->Events[] = $this->Event;	
				}
			}	
		}
		function GetCalendarTypeFromDatabase($SelectedValue) {
			$this->CalendarSelect = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
			$this->CalendarSelect->Name = 'CalendarID';
			$this->CalendarSelect->CssClass = 'SmallInput';
			$this->CalendarSelect->Attributes .= 'id="CalendarSelect"';
			$sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
			$sql->Clear();
			$sql->SetMainTable('Calendar', 'Calendar');
			$sql->AddSelect(array('CalendarID', 'Title', 'RequireApproval' ,'Color'), 'Calendar');
			$ResultSet = $this->Context->Database->Select($sql, $this->Name, 'GetCalendarTypeFromDatabase', 'An error occurred while retrieving Calendar Type from database.');
			$this->CalendarSelect->AddOptionsFromDataSet($this->Context->Database,$ResultSet,'CalendarID','Title' );
			$this->CalendarSelect->SelectedValue = $SelectedValue;
		}
		function RenderRepeatTypeRadioList() {
			$this->RepeatList = $this->Context->ObjectFactory->NewObject($this->Context, 'Radio');
			$this->RepeatList->Name = 'RepeatType';
			$this->RepeatList->CssClass = '';
			$this->RepeatList->Attributes .= '';
			$this->RepeatList->AddOptionsFromAssociativeArray($this->RepeatType,'');
			$this->RepeatList->SelectedID = 'No';
		}
		function RenderRepeatWeekDaysCheckboxes() {
			while (list($key, $val) = each($this->WeekDays)) {
				$this->WeekDaysCheckboxes .= GetDynamicCheckbox('RepeatDay'.$key, $key, 0, '', $val, '','RepeatDay'.$key);
			}
		}
		function RenderRepeatMonthWeekNum () {
			$this->MonthWeekNum = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
			$this->MonthWeekNum->Name = 'MonthWeekNum';
			$this->MonthWeekNum->CssClass = 'SmallInput';
			$this->MonthWeekNum->Attributes .= 'id="MonthWeekNumSelect"';
			$this->MonthWeekNum->AddOption(1, $this->Context->GetDefinition('VcalMonthWeekFirst'));
			$this->MonthWeekNum->AddOption(2, $this->Context->GetDefinition('VcalMonthWeekSecond'));
			$this->MonthWeekNum->AddOption(3, $this->Context->GetDefinition('VcalMonthWeekThird'));
			$this->MonthWeekNum->AddOption(4, $this->Context->GetDefinition('VcalMonthWeekFourth'));
			$this->MonthWeekNum->AddOption(-1, $this->Context->GetDefinition('VcalMonthWeekLast'));
			$this->MonthWeekNum->SelectedValue = 1;
		}
		function SaveEventToDatabase() {
			$this->Event->GetPropertiesFromForm($this->Discussion);
			$this->Event->FormatPropertiesForDatabaseInput();
			$sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
			$sql->SetMainTable('Event', 'Event');
			$sql->AddFieldNameValue('CalendarID', $this->Event->CalendarID);
			$sql->AddFieldNameValue('UserID', $this->Event->UserID);
			$sql->AddFieldNameValue('DiscussionID', $this->Event->DiscussionID);
			$sql->AddFieldNameValue('Name', $this->Event->Name);
			$sql->AddFieldNameValue('StartDate', $this->Event->StartDate);
			$sql->AddFieldNameValue('EndDate', $this->Event->EndDate);
			$sql->AddFieldNameValue('StartTime', $this->Event->StartTime);
			$sql->AddFieldNameValue('EndTime', $this->Event->EndTime);
			$sql->AddFieldNameValue('Approved', $this->Event->Approved);
			$sql->AddFieldNameValue('DateCreated', $this->Event->DateCreated);
			$sql->AddFieldNameValue('AllDayEvent', $this->Event->AllDayEvent);
			if ($this->Event->EventID > 0) {
				$sql->AddWhere('Event','EventID','',$this->Event->EventID,'=');
				$this->Context->Database->Update($sql, $this->Name, 'SaveEventToDatabase', 'An error occurred while updating an event');
			}
			else {
				$this->Context->Database->Insert($sql, $this->Name, 'SaveEventToDatabase', 'An error occurred while saving a new event');
			}
			
		}
		
		function RenderEventForm() {
			if ($this->DiscussionID > 0) {
				$this->GetEventByDiscussionID($this->DiscussionID);	
			}
			$this->GetCalendarTypeFromDatabase($this->Event->CalendarID);
			$this->RenderRepeatTypeRadioList();
			$this->RenderRepeatWeekDaysCheckboxes();
			$this->RenderRepeatMonthWeekNum ();
			include(VCAL_ROOT. '/theme/Theme.EventForm.php');
		}
	}
?>
